﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Windows.Forms;

namespace iPdf
{
    class OutputFolderHistoryDropDown: TataDropDown
    {
        #region Fields
        private string defaultOutputFolder;
        private string outputFolderHistoryPath = Helper.RoamingDir + "OutputFolderHistory.xml";
        private string outputFolderTag = "OutputFolder";
        private string defaultOutputFolderAttr = "Default";
        #endregion

        #region Ctor

        public OutputFolderHistoryDropDown()
            :base()
        {
            this.ButtonClick += new EventHandler(OutputFolderHistoryDropDown_ButtonClick);
        }

        #endregion

        #region Methods
        public string DefaultOutputFolder
        {
            get
            {
                if (!Directory.Exists(defaultOutputFolder))
                {
                    defaultOutputFolder = Helper.MyDocumentsDir;
                    AddItem(defaultOutputFolder, OutputFolderHistoryDropDownItem_Click);
                    SelectedItem = defaultOutputFolder;

                    Global.ShowNotification(
                        "The folder " + Helper.QuoteString(defaultOutputFolder) +
                        " doesn't exist and it'll be reset to " +
                        Helper.QuoteString("My Documnets"));
                }

                return defaultOutputFolder;
            }
            set
            {
                if (!Directory.Exists(value))
                {
                    return;
                }

                if (defaultOutputFolder != value && !Contains(value))
                {
                    AddItem(value, OutputFolderHistoryDropDownItem_Click);
                }

                defaultOutputFolder = value;
                SelectedItem = defaultOutputFolder;
                Global.Grid.UpdateOutputFolder(defaultOutputFolder);
            }
        }

        public void Load()
        {
            defaultOutputFolder = Helper.MyDocumentsDir;
            List<string> items = new List<string>();
            if (File.Exists(outputFolderHistoryPath))
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(outputFolderHistoryPath);

                XmlNodeList nodeList = xmlDoc.GetElementsByTagName(outputFolderTag);
                foreach (XmlElement node in nodeList)
                {
                    string item = node.InnerText;
                    if (!string.IsNullOrEmpty(item) &&
                        !Contains(item) &&
                        Directory.Exists(item))
                    {
                        items.Add(item);
                    }

                    if (node.GetAttribute(defaultOutputFolderAttr) == "true")
                    {
                        defaultOutputFolder = item;
                    }
                }
            }

            if (!items.Contains(defaultOutputFolder))
            {
                items.Add(defaultOutputFolder);
            }

            AddStringArray(items.ToArray(), OutputFolderHistoryDropDownItem_Click);
            SelectedItem = defaultOutputFolder;
        }

        public void Save()
        {
            XmlDocument xmlDoc = Helper.CreateXmlDocument();
            XmlElement root = xmlDoc.CreateElement("Tata");
            xmlDoc.AppendChild(root);
            XmlElement outputFolderHistory = xmlDoc.CreateElement("OutputFolderHistory");
            root.AppendChild(outputFolderHistory);
            string[] items = ToStringArray();
            foreach (string item in items)
            {
                XmlElement xmlEle = xmlDoc.CreateElement(outputFolderTag);
                xmlEle.InnerText = item;

                if (item == defaultOutputFolder)
                {
                    xmlEle.SetAttribute(defaultOutputFolderAttr, "true");
                }

                outputFolderHistory.AppendChild(xmlEle);
            }

            xmlDoc.Save(outputFolderHistoryPath);
        }

        private void OutputFolderHistoryDropDownItem_Click(object sender, EventArgs e)
        {
            Elegant.Ui.ToggleButton menuItem = (Elegant.Ui.ToggleButton)sender;

            string folder = menuItem.Text;
            if (!Directory.Exists(folder))
            {
                if (folder == DefaultOutputFolder)
                {
                    RefreshMenuItems();
                }

                Global.ShowNotification("The folder " + Helper.QuoteString(folder) +
                        " doesn't exist any more and thus it's been removed.");

                return;
            }

            DefaultOutputFolder = folder;
        }

        private void OutputFolderHistoryDropDown_ButtonClick(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
            folderBrowser.SelectedPath = defaultOutputFolder;
            if (folderBrowser.ShowDialog() == DialogResult.OK)
            {
                DefaultOutputFolder = Helper.RegularizeDirectory(folderBrowser.SelectedPath);
            }
        }
        #endregion
    }
}
